/**
 * 
 */
package model.persistence.mysql;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.core.torneo.Gara;
import model.core.utente.Commissario;
import model.core.utente.Studente;
import model.persistence.StudenteDAO;

/**
 * @author Giuseppe Fusco (fusgius19@gmail.com)
 *
 */
public class StudenteDAO_MySQL extends MySQLDomainDAO implements StudenteDAO{
	
	
	/**
	 * @param dbms
	 * @throws SQLException 
	 */
	public StudenteDAO_MySQL(MySQLDBMS dbms) throws SQLException {
		super(dbms);
		// TODO Auto-generated constructor stub
	}

	/* (non-Javadoc)
	 * @see model.facade.persistence.PersistenceCommand#insert(java.lang.Object)
	 */
	@Override
	public boolean insert(Object...obj) {
		// TODO Auto-generated method stub
		if(obj[0] instanceof Studente){
			try {
				synchronized (obj[0]) {
					Studente s = (Studente) obj[0];
					PreparedStatement r = mys.getPreparedStatement("insert into studente(nome,  cognome,  matricola" +
							",password,anno_corso,anno_immatricolazione,data_nascita,tipo) values(?,?,?,?,?,?,?,?)");
						r.setString(1, s.getNome());
						r.setString(2, s.getCognome());
						r.setString(3, s.getMatricola());
						r.setString(4, s.getPassword());
						r.setInt(5, s.getAnno_corso());
						r.setInt(6, s.getAnno_immatricolazione());
						r.setDate(7, new Date(s.getData_nascita().getTimeInMillis()));
						r.setString(8, s instanceof Commissario?"COMMISSARIO":"STUDENTE");
						r.execute();
						r.close();
						return true;	
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				return false;
			}
		}
		else
			return false;
	}

	/* (non-Javadoc)
	 * @see model.facade.persistence.PersistenceCommand#delete(java.lang.Object)
	 */
	@Override
	public boolean delete(Object obj) {
		if(obj instanceof Gara){
			Studente g = (Studente)obj;
			try {
				mys.executeUpdate("delete from studente where matricola=\""+g.getMatricola()+"\"");
				return true;
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}else return false;
	}

	/* (non-Javadoc)
	 * @see model.facade.persistence.PersistenceCommand#update(java.lang.Object, java.lang.String[])
	 */
	@Override
	public boolean update(Object obj) {
		// TODO Auto-generated method stub
		if(obj instanceof Studente){
			try {
				synchronized (obj) {
					Studente s = (Studente) obj;
					ResultSet r = mys.getResult("select * from studente where matricola=\""+s.getMatricola()+"\"");
					if(r.first()){
						r.updateString("nome", s.getNome());
						r.updateString("cognome", s.getCognome());
						r.updateString("matricola", s.getMatricola());
						r.updateString("password", s.getPassword());
						r.updateInt("anno_corso", s.getAnno_corso());
						r.updateInt("anno_immatricolazione", s.getAnno_immatricolazione());
						r.updateDate("data_nascita", new Date(s.getData_nascita().getTimeInMillis()));
						r.updateString("tipo", s instanceof Commissario?"COMMISSARIO":"STUDENTE");
						r.updateRow();
						r.close();
						return true;
					}else
						return false;	
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				return false;
			}
		}
		else
			return false;
	}

	/* (non-Javadoc)
	 * @see model.facade.persistence.PersistenceCommand#retrieve(java.lang.Object[])
	 */
	@Override
	public Object retrieve(Object... properties) {
		// TODO Auto-generated method stub
		if(properties!=null && properties.length==2){
			Object o1=properties[0];
			Object o2=properties[1];
			if(o1 instanceof String && o2 instanceof String){
				return MySQLUtentiQueryUtility.getStudente((String)o1, (String)o2, mys);
			}else
				return null;
		}
		else
			return null;
	}
	
	/**
	 * @param text
	 * @return
	 * @throws SQLException 
	 */
	public List<Studente> load(String text) throws SQLException{
		List<Studente> list = new ArrayList<Studente>();
		ResultSet r = mys.getResult("select * from studente where (nome like\"%"+text+"%\" or cognome like\"%"+text+"%\" or " +
				"data_nascita like\"%"+text+"%\" or anno_corso like\"%"+text+"%\" or anno_immatricolazione like\"%"+text+"%\" or matricola like\"%"+text+"%\" or " +
						"password like\"%"+text+"%\" or tipo like\"%"+text+"%\")");
		while(r.next()){
			list.add((Studente) retrieve(r.getString("matricola"),r.getString("password")));
		}
		r.close();
		return list;
	}

}
